package com.example.nirvana.dataStructure;

/**
 * 栈
 */
public class MyStack {
    // 存放栈数据的数组
    private int[] stack;
    // 栈顶和栈底的标识以及数组的大小
    private final int SIZE;
    private final int BOTTOM = 0;
    private int top;

    public MyStack(int size) {
        SIZE = size;
        stack = new int[SIZE];
        top = BOTTOM;
    }

    /**
     * 判断栈是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return top == BOTTOM;
    }

    /**
     * 判断栈是否为满栈
     *
     * @return
     */
    public boolean isFull() {
        return top == SIZE;
    }

    /**
     * 入栈
     *
     * @param data
     * @throws IllegalStateException
     */
    public synchronized void push(int data) throws IllegalStateException {
        if(!isFull()) {
            stack[top++] = data;
        }else {
            throw new IllegalStateException("Stack is overload.");
        }
    }

    /**
     * 出栈
     *
     * @return
     */
    public synchronized int pop() throws IllegalStateException {
        if(!isEmpty()) {
            return stack[--top];
        }else {
            throw new IllegalStateException("Stack is empty.");
        }
    }


}
